/**
 * Moondance UML modeling tool
 * 
 * @author Dascalu Laurentiu (dascalu.laurentziu@gmail.com).
 * 
 * This program is free software; you can redistribute it and
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ro.pub.cs.se.moondance;

import java.io.InputStream;
import java.net.URL;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.dialogs.WizardResourceImportPage;
import org.eclipse.ui.ide.IDE;
import org.osgi.framework.Bundle;

public class DesignPatternsPage extends WizardResourceImportPage {
	private static String RELATIVE_PATH = "resources/DesignPatterns/";
	private static String DESIGN_PATTENRS[] = { "Bridge", "Builder",
			"Composite", "Iterator", "Singleton", "Strategy", };
	private static String EXTENSION = ".uml.moondance";

	protected DesignPatternsPage(String name, IStructuredSelection selection) {
		super(name, selection);
	}

	@Override
	protected void createSourceGroup(Composite parent) {

	}

	@Override
	protected ITreeContentProvider getFileProvider() {
		return null;
	}

	@Override
	protected ITreeContentProvider getFolderProvider() {
		return null;
	}

	public String getDestinationPath() {
		IPath fullPath = getContainerFullPath();

		if (fullPath == null)
			return null;
		return fullPath.toOSString();
	}

	public boolean finish() {
		if (getDestinationPath() == null)
			return false;

		String projectName = getDestinationPath();
		Bundle bundle = Platform.getBundle("ro.pub.cs.se.moondance");

		try {
			IWorkspace workspace = ResourcesPlugin.getWorkspace();
			workspace.getPathVariableManager().getPathVariableNames();

			for (int i = 0; i < DESIGN_PATTENRS.length; i++) {
				Path path = new Path(RELATIVE_PATH + DESIGN_PATTENRS[i]
						+ EXTENSION);
				URL fileURL = FileLocator.find(bundle, path, null);
				InputStream in = fileURL.openStream();

				IPath location = Path.fromOSString(DESIGN_PATTENRS[i]
						+ EXTENSION);
				IProject project = workspace.getRoot().getProject(projectName);
				IFile newFile = project.getFile(location);

				if (newFile.exists())
					newFile.delete(true, MoondanceData.DEFAULT_MONITOR);

				newFile.create(in, true, MoondanceData.DEFAULT_MONITOR);

				IWorkbenchPage page = MoondanceData.getWorkbench()
						.getActiveWorkbenchWindow().getActivePage();
				IDE.openEditor(page, newFile, true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return true;
	}
}
